/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webanovare.ruzzle.mgr.impl;

import com.webanovare.ruzzle.game.Ruzzle;
import com.webanovare.ruzzle.misc.Constants;
import com.webanovare.ruzzle.misc.Player;
import com.webanovare.ruzzle.misc.Trie;
import java.awt.Point;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

/**
 *
 * @author ekbga
 */
public class ActiveGame extends GameState {
    final private Ruzzle game;
    private int opponentPoints = 0;
    private int ownerPoints = 0;
    private Map<String, Integer> ownerWords, opponentWords;
    private String language;
    
    public ActiveGame(IHandleState g, File wordList, File letterFrequency) {
        super(g);
        this.game = new Ruzzle(super.g.getSize(), wordList, letterFrequency);
    }
    
    public ActiveGame(IHandleState g, String language) {
        super(g);
        
        this.ownerWords = new HashMap<String, Integer>();
        this.opponentWords = new HashMap<String, Integer>();
        
        this.language = language;
        
        File wordList = null;
        File freq = null;
        
        String freqPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/WEB-INF/" + Constants.FREQUENCY_FILE_NAME + this.language);
        freq = new File(freqPath);
        
        String wordListPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/WEB-INF/" + Constants.WORDLIST_FILE_NAME + this.language + ".txt");
        wordList = new File(wordListPath);
            
        this.game = new Ruzzle(super.g.getSize(), wordList, freq);
    }

    @Override
    public void accept() {
        throw new UnsupportedOperationException("Not suppoerted by the state ActiveGame");
    }

    @Override
    public void decline() {
        throw new UnsupportedOperationException("Not suppoerted by the state ActiveGame");
    }

    @Override
    public void challenge(Player p) {
        throw new UnsupportedOperationException("Not suppoerted by the state ActiveGame");
    }

    @Override
    public List<Character> getBoard() {
        return this.game.getBoard();
    }
    
    public Character[][] getBoardMatrix() {
        return this.game.getBoardMatrix();
    }

    @Override
    public boolean addWord(List<Point> ps, boolean opponent) {
        Map<String, Integer> tmp = opponent ? this.opponentWords : this.ownerWords;
        
        String word = this.game.getWord(ps);
        if(!tmp.containsKey(word)) {
            if(this.game.validEntry(ps)) {
                int points = this.game.calculatePoints(word);
                if(opponent) {
                    this.opponentWords.put(word, points);
                    this.opponentPoints += points;
                } else {
                    this.ownerWords.put(word, points);
                    this.ownerPoints += points;
                }

                return true;
            }
        }
        
        return false;
    }

    @Override
    public int getOpponentPoints() {
        return this.opponentPoints;
    }

    @Override
    public int getOwnerPoints() {
        return this.ownerPoints;
    }

    @Override
    public Map<String, Integer> getOwnerStatistics() {
        return this.ownerWords;
    }

    @Override
    public Map<String, Integer> getOpponentStatistics() {
        return this.opponentWords;
    }

    @Override
    public Trie getTrie() {
        return this.game.getTrie();
    }
}
